#ifndef CBTENGINE_H_
#define CBTENGINE_H_

#include <bt_sock.h>
#include <btextnotifiers.h>
#include <es_sock.h>
#include <btdevice.h>
#include <e32base.h>
#include "NmeaParser.h"
#include "Common.h"
#include "MGpsObserver.h"

#define KBUFFMAXLEN 128

#ifdef __LOGGER
	#include <flogger.h>
#endif
/* hack for switch on/off bluetooth 
 * link against BTENG.LIB copied from SDK 1.2
 * http://mikie.iki.fi/symbian/reverse.html
 * http://discussion.forum.nokia.com/forum/showthread.php?t=48926&highlight=switch+bluetooth
 */
class MBTMCMSettingsCB
{
};
class CBTMCMSettings
{
public:

   static CBTMCMSettings* NewLC(MBTMCMSettingsCB* x);
   static CBTMCMSettings* NewL(MBTMCMSettingsCB* x);

   void SetPowerStateL(TBool a, TBool b);
   void GetPowerStateL(TBool& a); /* it doesn't work on mine 6600 */
   TInt GetConnectionStatus(int &a);
};
/* end hack bt */

enum TBTStatus
{
	EBtIdle,
	EBtConnecting,
	EBtReconnecting,
	EBtConnected,
};

enum TNmeaSentenceStatus
{
	EEmpty,
	EPartial,
	ECompleate
};

class CBTEngine: public CActive, MGpsObserver
{
public:

	// From base class
	virtual void getData(TReal32& aLatitude, TReal32& aLongitude, TUint& aGpsFix);

	// Public funcions
	CBTEngine(const TBTDevAddr& aAddr, TUint aPort, MDisplayObserver& aDisplay);
	static void SelectBtDeviceL(TBTDeviceResponseParamsPckg& aResponse);
	static CBTEngine* NewL(const TBTDevAddr& aAddr, TUint aPort, MDisplayObserver& aDisplay);
	virtual ~CBTEngine();
	void ConnectL();
	void Disconnect();
	TBool isConnected();
	void setBtDevice(const TBTDevAddr& aAddr, const TUint& aPort);
	
private:
	void ConstructL();
	void SwitchBtOn();
	void SwitchBtOff();
	void FillBuffer();
	void FindNmeaSentence();
	void ParseNmea();
	void updateScreen();
	
	//CActive methods
	void DoCancel(); 
    void RunL();
	
	TBTStatus iState;
	RSocketServ iSocketServ;
	RSocket iSock;
	THostName iName;
	TBTDevAddr iAddr;
	TInt iPort;
	MDisplayObserver& iDisplay;
	RTimer iTimmer;
	TBuf8<KBUFFMAXLEN> iGpsBuffer;
	TBuf8<KBUFFMAXLEN> iNmeaSentence;
	TNmeaSentenceStatus iNmeaSentenceStatus;
	/* for parsing nmea */
	TInt iPos1;
	TInt iPos2;
	CNmeaParser iNmeaParser;
	TReal iUctTime;
	TReal iLatitude;
	TReal iLongitude;
	TInt iGpsFix;
	TInt iNumSat;
	TReal iAltitude;
	
	
	#ifdef __LOGGER
		RFileLogger iLog;
	#endif
	#ifdef __EMULATOR
		TInt iItem; 
	#endif

	CBTMCMSettings * iBTMCMSettings;

};



#endif /*CBTENGINE_H_*/
